package 网络编程.TCPFILEECHO3;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-05
 * Time: 13:53
 */
public class StreamUtil {

    //将输入流的数据转化成字节数组返回
    public static byte[] streamByteArray(InputStream is) throws IOException {
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len = 0;
        while((len = is.read(bytes))!=-1){
            byteArrayOutputStream.write(bytes,0,len);
        }
        byte[] array = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();
        return array;
    }
    public static String streamToString(InputStream os) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(os));
        String len;
        StringBuilder stringBuilder = new StringBuilder();
        while((len = bufferedReader.readLine())!=null){
            stringBuilder.append(len +"\r\n");
        }
        return stringBuilder.toString();
    }


}
